/**
 * comment module
 */
Narato.M("cmt",(function($){
	var p={},pub={};

	p.initQuote=function(){
		$('a.quote').livequery("click", function () {
			var $cmt=$(this).parents(p.opts.css_cmt),
				$c = $cmt.find('.comment-text'),
				$author = $cmt.find('.comment-author'),
				$cid = $cmt.find('.comment-id').attr('href');

			$(p.opts.id_target).appendVal('<blockquote>\n<a href="' + $cid + '">\n<strong><em>' + $author.html() + ':</em></strong>\n</a>\n ' + $c.html() + '</blockquote>').focus();
			return false;
		});		
	};

	p.initReply=function(){
		// replaces wp's comment reply script
		$("a.reply").livequery("click", function () {
			var linkClass = $(this).attr('id'),
				pos = linkClass.lastIndexOf('-'),
				targetID = linkClass.substr(++pos);

			$("#respond").hide();

			$('#comment_parent').attr('value',targetID);

			$("#cancel-reply").show();
			$("#respond").appendTo('#comment-body-'+targetID).show(0,function(){

				// move cursor in textarea, at the end of the text
				$('#comment').each(function(){
					if (this.createTextRange) {
						var r = this.createTextRange();
						r.collapse(false);
						r.select();
					};
					$(this).focus();
				});

			});

			return false;
		});	
	};

	p.initCancel=function(){
		// cancel-reply
		$("#cancel-reply").livequery("click", function (event) {
			$("#respond").hide();
			$('#comment_parent').attr('value','0');

			$("#cancel-reply").hide();
			$("#respond").appendTo('#section-comments').show(0,function(){

				// move cursor in textarea, at the end of the text
				$('#comment').each(function(){
					if (this.createTextRange) {
						var r = this.createTextRange();
						r.collapse(false);
						r.select();
					};
					$(this).focus();
				});

			});
			return false;
		});	
	};


	p.initAjaxCmt=function(){
		if(!p.opts.ajax_cmt) return;

		$(".comment-navigation a").livequery("click", function () {
			$link = jQuery(this);
			$('.comment-navigation').before('<div id="pagination_status"></div>');

			var link_url = $link.attr('href'),
				ajax_url = link_url.split(/#.*$/)[0],
				status = $('#pagination_status');

			$.ajax({
				url: ajax_url,
				type: "GET",
				data: ({action: 'commentnavi'/*, page: commentPage*/}),
				beforeSend: function() {
					status.empty();
					$link.removeAttr('href').addClass('loading').html('&nbsp;');
					$("#comments").css('opacity', 1).animate({ opacity: 0.5 }, 333,'easeOutQuart');
				}, // end beforeSubmit
				error: function(request){
					window.location=target_url;
					return false;
				},
				success: function(data) {
					try {
						var response = $("<div />").plainHtml(data),
							$comm = response.find('#comments-wrap').plainHtml();

						$("#cancel-reply").hide();
						$("#comments").find("#respond").appendTo('#section-comments');

						$('#comments-wrap').plainHtml($comm);
						$("#comments").css('opacity', 0.5).animate({ opacity: 1 }, 333,'easeOutQuart');
						status.empty();

					} catch (e) {
						status.attr("class","error").plainHtml(e);
					};//try

					if (window["AjaxEditComments"]) {AjaxEditComments.init();};
				}//success

			});//$.ajax

			return false;
		});

		// comment submit
		$('#respond form').before('<div id="comment_post_status"></div>');
		var form = $('#commentform'),
			status = $('#comment_post_status'),
			submit_text = $('#submit').val();

		form.submit(function(evt) {

			$(this).ajaxSubmit({

				beforeSubmit: function() {
					status.empty();
					$('#submit, #comment').attr('disabled','disabled');
					$('#submit').val(p.opts.lang_cmt[0]);
				}, // end beforeSubmit
				error: function(request){
					status.empty();
					var data=null;
					if (request.responseText.search(/<title>WordPress &rsaquo; Error<\/title>/) != -1) {
						data = request.responseText.match(/<p>(.*)<\/p>/);
						status.attr("class","error").plainHtml(data[1]);
					} else {
						data = request.responseText;
						status.attr("class","error").plainHtml(data[1]);
					}
					$('#submit').val(submit_text);
					$('#submit, #comment').removeAttr("disabled");
					return false;
				},//error
				success: function(data) {
					try {
						var response = $("<div />").plainHtml(data),
							$comm = response.find('#comments-wrap').plainHtml();
						// if is a nested comment
						$("#cancel-reply").hide();
						$("#comments").find("#respond").appendTo('#section-comments');

						$('#comments-wrap').plainHtml($comm);
						status.empty().attr("class","success").plainHtml(p.opts.lang_cmt[1]);
						$('#comment').val('');
						$('#submit').val(p.opts.lang_cmt[2]);
						$('#submit, #comment').removeAttr("disabled");
						$('#comment_parent').attr('value', '0');

					} catch (e) {
						$('#submit, #comment').removeAttr("disabled");
						status.attr("class","error").plainHtml(e);
					};//try

					if (window["AjaxEditComments"]){ AjaxEditComments.init();};

				}//success

			});

			return false;

		});//form.submit

	};


	/* public methods
	++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
	pub.init=function(opts){
		
		p.opts=$.extend({
			css_cmt:'li.comment',
			id_target:'#comment'
		},opts||{});

		$.fn.appendVal = function (txt) {
			return this.each(function () {
				this.value += txt;
			});
		};	

	};

	pub.onLoad=function(){
		p.initQuote();
		p.initReply();
		p.initCancel();
		p.initAjaxCmt();

		$(".comment-head").bubble({timeout: 0});
	};

	return pub;

})(jQuery));